package com.koro.vo;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.http.HttpStatus;

import java.io.Serializable;

@ApiModel(description = "返回信息")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class ResponseObject<T> implements Serializable {

    @ApiModelProperty(value = "状态码 0:成功", required = true)
    private int code;

    @ApiModelProperty("承载数据")
    private T data;

    @ApiModelProperty(value = "返回消息", required = true)
    private String msg;


    /**
     * 成功状态
     */
    public static final int okCode = 0;

    private ResponseObject(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
    }

    public static <T> ResponseObject<T> ok() {
        return ok(null, "操作成功");
    }

    public static <T> ResponseObject<T> ok(T data) {
        return ok(data, "操作成功");
    }

    public static <T> ResponseObject<T> ok(T data, String msg) {
        return ok(okCode, data, msg);
    }

    public static <T> ResponseObject<T> ok(int code, T data, String msg) {
        return new ResponseObject(code, data, data == null ? "操作成功" : msg);
    }


    public static <T> ResponseObject<T> error() {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), "未知异常，请联系管理员");
    }

    public static <T> ResponseObject<T> error(String msg) {
        return error(HttpStatus.INTERNAL_SERVER_ERROR.value(), msg);
    }

    public static <T> ResponseObject<T> error(int code, String msg) {
        return new ResponseObject(code, (Object) null, msg);
    }


    public int getCode() {
        return this.code;
    }

    public T getData() {
        return this.data;
    }

    public String getMsg() {
        return this.msg;
    }

    public void setCode(final int code) {
        this.code = code;
    }


    public void setData(final T data) {
        this.data = data;
    }

    public void setMsg(final String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "ResponseObject{" +
                "code=" + code +
                ", data=" + data +
                ", msg='" + msg + '\'' +
                '}';
    }

    public ResponseObject() {
    }
}
